package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    @GetMapping("/findAll")
    public Result findAll() {
        List<Permission> permissionList = permissionService.findAll();
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionList);
    }

    @GetMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(int roleId) {
        List<Integer> permissionIds = permissionService.findPermissionIdsByRoleId(roleId);
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permissionIds);
    }


    //添加权限
    @PostMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission){
        permissionService.addPermission(permission);
        return new Result(true, "添加菜单成功");
    }
    //分页查询
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult<Permission> pageResult =permissionService.findPage(queryPageBean);
        return new Result(true,"查询权限成功",pageResult);
    }
    //根据id查询，数据回显编辑页
    @RequestMapping("/findById")
    public Result findById(int id){
        Permission permission=permissionService.findById(id);
        return new Result(true,"权限回显成功",permission);
    }
    //修改权限
    @RequestMapping("/updatePermission")
    public Result updatePermission(@RequestBody Permission permission){
        permissionService.updatePermission(permission);
        return new Result(true,"权限修改成功");
    }
    //删除权限
    @RequestMapping("/deletePermissionById")
    public Result deletePermissionById(int id){
        permissionService.deletePermissionById(id);
        return new Result(true,"权限删除成功");
    }
    //查询所有权限
    @RequestMapping("/findPermisson")
    public Result findPermisson(){
        List<Permission>list=permissionService.findPermisson();
        return new Result(true,"查询所有权限成功",list);
    }
}
